package databasetranslation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Workshop;
import database.SQLServer;

public class WorkshopList extends DatabaseList<Workshop>{
	
	/**
	 * constructor, create's a WorkshopList
	 * 
	 * @param server for the database communication
	 */
	public WorkshopList(SQLServer server) {
		super(server, "workshoplist");
	}
	
	/**
	 * Get a copy of the list of Workshops.
	 * @return list of Workshops
	 */
	public List<Workshop> getWorkshops() {
		return new ArrayList<Workshop>(getEntries());
	}
	
	public enum BeforeAfterNow {
		BEFORE,
		AFTER;
		
		/**
		 * Check whether <code>date</code> is indeed before or after now, 
		 * depending on the enum value on which this method is called.
		 * 
		 * @param date
		 * @return true if BEFORE / AFTER now.
		 */
		public boolean isOk(Date date) {
			Date now = new Date();
			if (this == BEFORE) {
				return date.before(now);
			}
			else {
				 return date.after(now);
			}
		}
	}
	
	/**
	 * Returns the list of workshops that are before or after now.
	 *  
	 * @param beforeAfterNow
	 * @return list of Workshops (warning workshops are not copies)
	 */
	public List<Workshop> getFilteredWorkshops(BeforeAfterNow beforeAfterNow) {
		List<Workshop> selectedWorkshops = new ArrayList<Workshop>();
		for (Workshop workshop : getWorkshops()) {
			if (beforeAfterNow.isOk(workshop.getDate())) {
				selectedWorkshops.add(workshop);
			}
		}
		return selectedWorkshops;
	}	

	/**
	 * Get the upcoming workshops
	 * @return List of Workshops (warning workshops are not copies)
	 */
	public List<Workshop> getUpcomingWorkshops() {
		return getFilteredWorkshops(BeforeAfterNow.AFTER);
	}

	/**
	 * Get the past workshops
	 * @return List of Workshops (warning workshops are not copies)
	 */
	public List<Workshop> getArchivedWorkshops() {
		return getFilteredWorkshops(BeforeAfterNow.BEFORE);
	}
	
	/**
	 * Search in the workshops for the searchString.
	 * 
	 * A workshop is matched if the searchString is in:
	 * <ol>
	 *   <li>Title, or</li>
	 *   <li>docent, or</li>
	 *   <li>explaination</li>
	 * </ol>
	 * 
	 * @param searchString String to search for
	 * @return list of matching workshops
	 */
	public List<Workshop> search(String searchString) {
		List<Workshop> selectedWorkshops = new ArrayList<Workshop>();
		for (Workshop workshop : getWorkshops()) {
			if ( workshop.getTitle().contains(searchString) ||
			     workshop.getDocent().contains(searchString) ||
			     workshop.getExplaination().contains(searchString) ) {
				selectedWorkshops.add(workshop);
			}
		}
		return selectedWorkshops;
	}

	/**
	 * Create a Workshop from the current selected ResultSet row
	 * 
	 * @param rs ResultSet with a selected row of a Payment
	 * @throws ParseException 
	 * @return the Workshop created from the ResultSet
	 */
	@Override
	public Workshop convertResultSetToModel(ResultSet rs) throws SQLException {
		Workshop.DagDeel dagDeel = null;
		try {
			dagDeel = Workshop.DagDeel.parse(rs.getString("dagDeel"));
		} catch (ParseException e) {
			new RuntimeException("Invalid string: ");
		}
		return new Workshop(
			rs.getInt("id"),
			rs.getString("title"),
			rs.getDate("date"),
			dagDeel,
			rs.getDouble("price"),
			rs.getString("explaination"),
			rs.getInt("participantsMax"),
			rs.getString("docent")
		);
	}
	
	
}